package com.hlx.controller.user;

import com.hlx.entity.user.OrderForm;
import com.hlx.entity.user.Ticket;
import com.hlx.service.admin.StationService;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.service.user.*;
import com.hlx.utils.Result;
import com.hlx.vo.OrderFormVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/orderFormVo")
public class OrderFormVoController {
    @Autowired
    private TicketService ticketService;
    @Autowired
    private OrderFormService orderFormService;
    @Autowired
    private StationService stationService;
    @Autowired
    private TrainNumberService trainNumberService;
    @Autowired
    private TrainNumberStationService trainNumberStationService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/getOrderById/{personId}")
    public Result<List<OrderFormVo>> getAllOrderById(@PathVariable Integer personId){
        String passenger = customerService.getById(personId).getCustomerName();
        if(ticketService.getTicketByPersonId(personId).isEmpty()){
            return Result.error("查询失败");
        }
        // 根据个人Id获得获取车票信息
        List<Ticket> ticket = ticketService.getTicketByPersonId(personId);
        if(ticket.isEmpty()){
            return Result.error("查询失败");
        }
        // 获得车票id
        List<Integer> ticketId = ticket.stream().map(item->item.getId()).collect(Collectors.toList());
        // 根据车票id获得账单
        List<OrderForm> orderForms = orderFormService.getOrderForm(ticketId);
        if(orderForms.isEmpty()){
            return Result.error("查询失败");
        }
        // 组装OrderFormVo
        Iterator<Ticket> it = ticket.iterator();
        Iterator<OrderForm> ito = orderForms.iterator();
        List<OrderFormVo> orderFormVos = new ArrayList<>();
        while (it.hasNext()){
            Ticket ti = it.next();
            OrderForm of = ito.next();
            OrderFormVo orderFormVo = new OrderFormVo();
            orderFormVo.setId(of.getId());
            orderFormVo.setOrderNumber(of.getOrderNumber());
            orderFormVo.setTicketId(ti.getId());
            orderFormVo.setOrderStatus(of.getOrderStatus());
            orderFormVo.setPrice(ti.getPrice());
            orderFormVo.setTrainNumberName(trainNumberService.getById(ti.getTrainId()).getTrainNumberName());
            orderFormVo.setStartStation(stationService.getById(ti.getStartId()).getStationName());
            orderFormVo.setArriveStation(stationService.getById(ti.getArriveId()).getStationName());
            orderFormVo.setStartTime(trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(orderFormVo.getStartStation(),orderFormVo.getTrainNumberName()).getStartTime());
            orderFormVo.setArriveTime(trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(orderFormVo.getArriveStation(),orderFormVo.getTrainNumberName()).getArriveTime());
            orderFormVo.setPassenger(passenger);
            orderFormVo.setSeatType(ti.getSeatType());
            orderFormVo.setTrainDate(ti.getDate());
            orderFormVos.add(orderFormVo);
        }
        return Result.success(orderFormVos);
    }
}
